import { userLogin } from '@/api/user'
import {Message} from 'element-ui'
const user = {
  state: {
    id: '',
    email: '',
    title: '',
    workplace: '',
    jobtype: '',
    firstname: '',
    lastname: '',
    organization: '',
    address: '',
    zipcode: '',
    city: '',
    country: ''
  },
  mutations: {
    set_id: (state, id) => {
      state.id = id
    },
    set_email: (state, email) => {
      state.email = email
    },
    set_title: (state, title) => {
      state.title = title
    },
    set_workplace: (state, workplace) => {
      state.workplace = workplace
    },
    set_jobtype: (state, jobtype) => {
      state.jobtype = jobtype
    },
    set_firstname: (state, firstname) => {
      state.firstname = firstname
    },
    set_lastname: (state, lastname) => {
      state.lastname = lastname
    },
    set_organization: (state, organization) => {
      state.organization = organization
    },
    set_address: (state, address) => {
      state.address = address
    },
    set_zipcode: (state, zipcode) => {
      state.zipcode = zipcode
    },
    set_city: (state, city) => {
      state.city = city
    },
    set_country: (state, country) => {
      state.country = country
    }
  },
  actions: {
    Login ({commit, state}, formData) {
      return new Promise((resolve, reject) => {
        userLogin(formData).then(res => {
          console.log(res)
          if (res.data.code !== 0) {
            console.log(res.data.msg)
            Message({
              message: res.data.msg,
              type: 'error'
            })
          } else {
            commit('set_id', res.data.data.userId)
            commit('set_email', res.data.data.email)
            localStorage.setItem('userid', res.data.data.userId)
            localStorage.setItem('email', res.data.data.email)
            resolve(res)
          }
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
